// pages/home/home.js
import Category from "../../model/category";
import Service from "../../model/service";
// import Tim from "../../model/tim";
import { throttle } from "../../utils/utils";
const service = new Service()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:['全部服务','在提供','正在找'],
    currentTabIndex:0,
    categoryList:[
      {
        "id":1,
        "name":"保洁"
      },
      {
        "id":2,
        "name":"汽修"
      },
      {
        "id":3,
        "name":"疏通"
      },
    ],

    serviceList:[
      {
        id:1,
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },

      {
        id:2,
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招golang开发一名",
        price:"100000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0
      
      },
      {
        id:3,
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
      {
        cover_image:{
          id: 24,
          path:"https://pic1.zhimg.com/v2-979e9accdc753d951193cdebcab04711_720w.jpg"
        },
        category:{
          id:6,
          name:"护理"
        },
        title:"北京市招java开发一名",
        price:"10000.00",
        publisher:{
          id:1,
          nickname:"刘唯卿",
          avatar:"https://picx.zhimg.com/v2-f536786de4899a7bea47eb9543e65eaa_1440w.jpeg"
        },
        create_time:"2024-10-14",
        sales_volume:0,
        score:4.5
      
      },
    ],
    tabIndex:0,
    categoryId:0,
    loading:true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options)  {

    // Tim.getInstance()

    // this._getServiceList()

    // this._getCategoryList()

    this.setData({loading:false})


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * 下拉刷新
   */
 async onPullDownRefresh() {
    console.log('下拉刷新')
    // const serviceList = await service.reset().getServiceList()
    // this.setData({serviceList})
    
    wx.stopPullDownRefresh()
      
  },

  /**
   * 页面上拉触底事件的处理函数
   * 加载更多
   */
  async onReachBottom() {
    console.log("加载更多")


    // 获取下一页数据并且和当前数据合并
    if(!service.hasMoreData){
      return
    }

  

    // const serviceList = await service.getServiceList(this.data.categoryId,this.data.tabIndex)
    // this.setData({serviceList})
    

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },


  // 点击 tab页
  handleTabChange(event){
    
   const tabIndex = event.detail.index
   this.data.tabIndex = tabIndex
   //this._getServiceList()
  },

  /**
   * 点击分类
   * @param {} event 
   */
  handleCategoryChange:throttle(function (event){

    if(this.data.categoryId === event.currentTarget.dataset.categoryid){
      return
    }
  
    const categoryId = event.currentTarget.dataset.categoryid

    this.data.categoryId = categoryId
  
   
  }),

  // 查询服务列表
 async _getServiceList(){
   const serviceList = await service.reset().getServiceList(this.data.categoryId,this.data.tabIndex)
   
  },
  
  // 查询分类列表
 async _getCategoryList(){
    const categoryList = await Category.getCategoryListWithAll()

    this.setData({
      categoryList:serviceList
    })
  },
  handleSelectService:function(event){

    const service = event.currentTarget.dataset.service
    console.log(event)

    wx.navigateTo({
      url: '/pages/service-detail/service-detail?service_id=' + service.id
    });
      
  
  }



})


